package com.example.controller;

import com.example.pojo.Baobiao;
import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.service.BaobiaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@RequestMapping("/bao")
public class BaobiaoController {
    @Autowired
    private BaobiaoService baobiaoService;
    @PostMapping
    public Result baobiao(@RequestBody Baobiao baobiao){
        baobiaoService.add(baobiao);
        return Result.success();
    }
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize,
                       String metrname, String status,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageBean pageBean = baobiaoService.page(page, pageSize,metrname, status ,begin, end);
        return Result.success(pageBean);
    }
    @PutMapping
    public Result check_m(@RequestBody Baobiao baobiao){
        int b = baobiaoService.check_m(baobiao);
        if(b==-1){
            return Result.success();
        }else{
            return Result.error("该物料库存不足,还差  "+b+" 吨可满足该报表");
        }
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        baobiaoService.delete(id);
        return Result.success();
    }
    @GetMapping("/check")
    private Result get_check(String name, String num){
        int nu_m=Integer.parseInt(num);
        int nums=baobiaoService.getCheck(name,nu_m);
        return Result.success(nums);
    }
}
